//
//  UUIntentOrderDetailBasicInfoVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 01/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUIntentOrderDetailBasicInfoVC.h"
#import <UUFoundation/UUCityManager.h>
#import "UUResourceManager.h"
#import "UUUserManager.h"
#import "UUOrderDetailVC.h"

@interface UUIntentOrderDetailBasicInfoVC ()

@end

@implementation UUIntentOrderDetailBasicInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.order != nil);
    
    [self setupUI];
    
    CGSize fittingCompressedSize = [self.view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    self.preferredContentSize = fittingCompressedSize;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *carTypeLabel = [UILabel new];
    carTypeLabel.text = [GetCarCategoryManager() fetchCarCategoryWithCategoryId:self.order.carCategoryId].name;
    carTypeLabel.textAlignment = NSTextAlignmentCenter;
    carTypeLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:carTypeLabel];
    [carTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(25);
        make.top.mas_equalTo(25);
    }];
    
    NSMutableArray<UUFormItemMode *> *items = [NSMutableArray new];
    
    [items addObject:[UUFormItemMode itemWithTitle:@"出发时间" content:[self.order.departTime formatDateByFormatString:@"yyyy.MM.dd HH:mm"]]];
    [items addObject:[UUFormItemMode itemWithTitle:@"出发城市" content:[GetCityManager() fetchCityWithCityId:self.order.departure.toAddressModel.cityId].simpleName]];
    [items addObject:[UUFormItemMode itemWithTitle:@"到达城市" content:[GetCityManager() fetchCityWithCityId:self.order.arrival.toAddressModel.cityId].simpleName]];
    [items addObject:[UUFormItemMode itemWithTitle:@"出发地址" content:self.order.departure.toAddressModel.address]];
    
    if (self.order.approach.length > 0) {
        [items addObject:[UUFormItemMode itemWithTitle:@"途经地址"
                                               content:[[self.order.approach.toAddressModels valueForKey:@"address"]
                                                        componentsJoinedByString:@"\n"]]];
    }
    
    [items addObject:[UUFormItemMode itemWithTitle:@"到达地址" content:self.order.arrival.toAddressModel.address]];
    [items addObject:[UUFormItemMode itemWithTitle:@"数量" content:[NSString stringWithFormat:@"%ld辆", self.order.carNum]]];
    [items addObject:[UUFormItemMode itemWithTitle:@"天数" content:[NSString daysToDisplayString:self.order.days]]];
    
    if (self.order.extra.length > 0) {
        [items addObject:[UUFormItemMode itemWithTitle:@"备注" content:self.order.extra]];
    }
    
    UUFormView *formView = [[UUFormView alloc] initWithItems:items preferredMaxLayoutWidth:self.view.width - 40];
    [self.view addSubview:formView];
    [formView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(carTypeLabel.mas_bottom).offset(28);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    if (self.order.status == UUIntentOrderStatusCompleted && [[GetUserManager() fetchUserInfo].supplierId isEqual:self.order.supplierId]) {
        UIButton *orderDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        orderDetailButton.titleLabel.font = [GetResourceManager() iconFontWithSize:14];
        [orderDetailButton setTitle:GetResourceManager().seeOrderDetailIconText forState:UIControlStateNormal];
        [orderDetailButton setTitleColor:COMMON_TEXT_LIGHT_COLOR forState:UIControlStateNormal];
        [orderDetailButton addTarget:self action:@selector(enterOrderDetail) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:orderDetailButton];
        [orderDetailButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.left.right.mas_equalTo(0);
            make.top.equalTo(formView.mas_bottom).offset(10);
        }];
    }
    
    [self.view.subviews.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-19);
    }];
}

- (void)enterOrderDetail {
    UUOrderDetailVC *orderDetailVC = [UUOrderDetailVC instanceOfAppropriate];
    orderDetailVC.orderId = self.order.orderId;
    [self.navigationController pushViewController:orderDetailVC animated:YES];
}

@end
